package com.wutong.eams.other;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ConditionBuilder {
    @Getter
    public enum Operator {
        EQUAL("="), NOT_EQUAL("<>"), GREATER_THAN(">"),
        LESS_THAN("<"), GREATER_THAN_OR_EQUAL(">="),
        LESS_THAN_OR_EQUAL("<="), LIKE("LIKE"),
        IN("IN"), NOT_IN("NOT IN"), BETWEEN("BETWEEN"),
        IS_NULL("IS NULL"), IS_NOT_NULL("IS NOT NULL");

        private final String operator;

        Operator(String operator) {
            this.operator = operator;
        }
    }

    private final List<String> conditions = new ArrayList<>();
    private final List<Object> parameters = new ArrayList<>();
    private boolean hasWhere = false;

    public static final ConditionBuilder NONE_CONDITION = new ConditionBuilder();

    public ConditionBuilder where(String column, Operator operator, Object value) {
        return addCondition("WHERE", column, operator, value);
    }

    public ConditionBuilder of(String prefix, String column, Operator operator, Object value) {
        return addCondition(prefix, column, operator, value);
    }

    public ConditionBuilder createGroup(String column, Operator operator, Object value) {
        return addCondition("", column, operator, value);
    }

    public ConditionBuilder and(String column, Operator operator, Object value) {
        return addCondition("AND", column, operator, value);
    }

    public ConditionBuilder or(String column, Operator operator, Object value) {
        return addCondition("OR", column, operator, value);
    }

    public ConditionBuilder andGroup(ConditionBuilder group) {
        conditions.add("AND (" + group.build() + ")");
        parameters.addAll(List.of(group.getParameters()));
        return this;
    }

    public ConditionBuilder orGroup(ConditionBuilder group) {
        conditions.add("OR (" + group.build() + ")");
        parameters.addAll(List.of(group.getParameters()));
        return this;
    }

    public String build() {
        return String.join(" ", conditions);
    }

    public Object[] getParameters() {
        return parameters.toArray();
    }

    private ConditionBuilder addCondition(String prefix, String column, Operator operator, Object value) {
        String condition = switch (operator) {
            case IN, NOT_IN -> column + " " + operator.getOperator() + " (?)";
            case BETWEEN -> column + " " + operator.getOperator() + " ? AND ?";
            case IS_NULL, IS_NOT_NULL -> column + " " + operator.getOperator();
            default -> column + " " + operator.getOperator() + " ?";
        };
        // 处理第一个条件（需要添加WHERE）
        if (!hasWhere && "WHERE".equals(prefix)) {
            conditions.add(prefix + " " + condition);
            hasWhere = true;
        } else {
            conditions.add(prefix + " " + condition);
        }
        if (value != null) {
            parameters.add(value);
        }
        log.debug("目前的条件：{}", conditions);
        return this;
    }
}
